import cgi;
import os
import urllib

print "Content-Type: application/json\n"

try:
    form = cgi.FieldStorage()
    filepath = urllib.unquote(form.getvalue("filepath"))

    dirs=[]
    files=[]
    for f in os.listdir(filepath):
        uri = os.path.join(filepath,f)
        label = os.path.basename(uri)
        #item = "{ 'label': '%s', 'uri': '%s'}" % (label, uri)
        item = '{"label":"%s", "uri":"%s"}' % (label, uri)
        if os.path.isdir(uri):
            dirs.append(item)
        else:
            files.append(item)

    print '{ "directories": [' + ",".join(dirs) + '], "files": [' + ",".join(files) + '] }'

except Exception,e:
    print str(e) + "\n"